package logic

import (
	"context"
	"product/models/mysql"

	"product/productrpc/internal/svc"
	"product/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderAddLogic {
	return &OrderAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderAddLogic) OrderAdd(in *productrpc.OrderAddRequest) (*productrpc.OrderAddResponse, error) {
	// todo: add your logic here and delete this line

	//var order mysql.Order
	//mysql.DB.Debug().Where("id = ?", order.ID).Find(&order)
	//if order.ID == 0 {
	//	return nil, errors.New("当前订单已存在")
	//}

	order := mysql.Order{
		UserId:      int(in.UserId),
		ProductId:   int(in.ProductId),
		OrderName:   in.OrderName,
		OrderNumber: in.OrderNumber,
		OrderNum:    in.OrderNum,
	}
	mysql.DB.Debug().Create(&order)

	return &productrpc.OrderAddResponse{
		OrderId: int64(order.ID),
	}, nil
}
